<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>boost/python/call_method.hpp</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Boost.Python Reference Manual">
<link rel="up" href="../function_invocation_and_creation.html" title="Chapter 4. Function Invocation and Creation">
<link rel="prev" href="boost_python_call_hpp.html" title="boost/python/call.hpp">
<link rel="next" href="boost_python_data_members_hpp.html" title="boost/python/data_members.hpp">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_call_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function_invocation_and_creation.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_data_members_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="function_invocation_and_creation.boost_python_call_method_hpp"></a><a class="link" href="boost_python_call_method_hpp.html" title="boost/python/call_method.hpp">boost/python/call_method.hpp</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="boost_python_call_method_hpp.html#function_invocation_and_creation.boost_python_call_method_hpp.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="boost_python_call_method_hpp.html#function_invocation_and_creation.boost_python_call_method_hpp.function_call_method">Function
        <code class="computeroutput"><span class="identifier">call_method</span></code></a></span></dt>
<dt><span class="section"><a href="boost_python_call_method_hpp.html#function_invocation_and_creation.boost_python_call_method_hpp.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_call_method_hpp.introduction"></a><a class="link" href="boost_python_call_method_hpp.html#function_invocation_and_creation.boost_python_call_method_hpp.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<p>
          &lt;boost/python/call_method.hpp&gt; defines the call_method family of
          overloaded function templates, used to invoke callable attributes of Python
          objects from C++.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_call_method_hpp.function_call_method"></a><a class="link" href="boost_python_call_method_hpp.html#function_invocation_and_creation.boost_python_call_method_hpp.function_call_method" title="Function call_method">Function
        <code class="computeroutput"><span class="identifier">call_method</span></code></a>
</h3></div></div></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">A1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">A2</span><span class="special">,</span> <span class="special">...</span> <span class="keyword">class</span> <span class="identifier">An</span><span class="special">&gt;</span>
<span class="identifier">R</span> <span class="identifier">call_method</span><span class="special">(</span><span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">self</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">method</span><span class="special">,</span> <span class="identifier">A1</span> <span class="keyword">const</span><span class="special">&amp;,</span> <span class="identifier">A2</span> <span class="keyword">const</span><span class="special">&amp;,</span> <span class="special">...</span> <span class="identifier">An</span> <span class="keyword">const</span><span class="special">&amp;)</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Requires</span></dt>
<dd><p>
                <code class="computeroutput"><span class="identifier">R</span></code> is a pointer type,
                reference type, or a complete type with an accessible copy constructor
              </p></dd>
<dt><span class="term">Effects</span></dt>
<dd><p>
                Invokes <code class="computeroutput"><span class="identifier">self</span><span class="special">.</span><span class="identifier">method</span><span class="special">(</span><span class="identifier">a1</span><span class="special">,</span> <span class="identifier">a2</span><span class="special">,</span> <span class="special">...</span><span class="identifier">an</span><span class="special">)</span></code> in Python, where <code class="computeroutput"><span class="identifier">a1</span><span class="special">...</span><span class="identifier">an</span></code>
                are the arguments to <code class="computeroutput"><span class="identifier">call_method</span><span class="special">()</span></code>, converted to Python objects. For
                a complete semantic description, see this page.
              </p></dd>
<dt><span class="term">Returns</span></dt>
<dd><p>
                The result of the Python call, converted to the C++ type <code class="computeroutput"><span class="identifier">R</span></code>.
              </p></dd>
<dt><span class="term">Rationale</span></dt>
<dd><p>
                <code class="computeroutput"><span class="identifier">call_method</span></code> is critical
                to implementing C++ virtual functions which are overridable in Python,
                as shown by the example below.
              </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_call_method_hpp.example"></a><a class="link" href="boost_python_call_method_hpp.html#function_invocation_and_creation.boost_python_call_method_hpp.example" title="Example">Example</a>
</h3></div></div></div>
<p>
          The following C++ illustrates the use of <code class="computeroutput"><span class="identifier">call_method</span></code>
          in wrapping a class with a virtual function that can be overridden in Python:
          C++ Module Definition
        </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="keyword">class</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">call_method</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">def</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">utility</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cstring</span><span class="special">&gt;</span>

<span class="comment">// class to be wrapped</span>
<span class="keyword">class</span> <span class="identifier">Base</span>
<span class="special">{</span>
 <span class="keyword">public</span><span class="special">:</span>
   <span class="keyword">virtual</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">class_name</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="string">"Base"</span><span class="special">;</span> <span class="special">}</span>
   <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">Base</span><span class="special">()</span> <span class="special">{};</span>
<span class="special">};</span>

<span class="keyword">bool</span> <span class="identifier">is_base</span><span class="special">(</span><span class="identifier">Base</span><span class="special">*</span> <span class="identifier">b</span><span class="special">)</span>
<span class="special">{</span>
   <span class="keyword">return</span> <span class="special">!</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">strcmp</span><span class="special">(</span><span class="identifier">b</span><span class="special">-&gt;</span><span class="identifier">class_name</span><span class="special">(),</span> <span class="string">"Base"</span><span class="special">);</span>
<span class="special">}</span>

<span class="comment">// Wrapper code begins here</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>

<span class="comment">// Callback class</span>
<span class="keyword">class</span> <span class="identifier">Base_callback</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">Base</span>
<span class="special">{</span>
 <span class="keyword">public</span><span class="special">:</span>
   <span class="identifier">Base_callback</span><span class="special">(</span><span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">self</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">m_self</span><span class="special">(</span><span class="identifier">self</span><span class="special">)</span> <span class="special">{}</span>

   <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">class_name</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">call_method</span><span class="special">&lt;</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*&gt;(</span><span class="identifier">m_self</span><span class="special">,</span> <span class="string">"class_name"</span><span class="special">);</span> <span class="special">}</span>
   <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">Base_name</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">Base</span><span class="special">::</span><span class="identifier">class_name</span><span class="special">();</span> <span class="special">}</span>
 <span class="keyword">private</span><span class="special">:</span>
   <span class="identifier">PyObject</span><span class="special">*</span> <span class="keyword">const</span> <span class="identifier">m_self</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">my_module</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">def</span><span class="special">(</span><span class="string">"is_base"</span><span class="special">,</span> <span class="identifier">is_base</span><span class="special">);</span>

    <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Base</span><span class="special">,</span><span class="identifier">Base_callback</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">noncopyable</span><span class="special">&gt;(</span><span class="string">"Base"</span><span class="special">)</span>
        <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"class_name"</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">Base_callback</span><span class="special">::</span><span class="identifier">Base_name</span><span class="special">)</span>
        <span class="special">;</span>

<span class="special">}</span>
</pre>
<p>
          Python code:
        </p>
<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">from</span> <span class="identifier">my_module</span> <span class="identifier">import</span> <span class="special">*</span>
<span class="special">&gt;&gt;&gt;</span> <span class="keyword">class</span> <span class="identifier">Derived</span><span class="special">(</span><span class="identifier">Base</span><span class="special">):</span>
<span class="special">...</span>    <span class="identifier">def</span> <span class="identifier">__init__</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span>
<span class="special">...</span>       <span class="identifier">Base</span><span class="special">.</span><span class="identifier">__init__</span><span class="special">(</span><span class="identifier">self</span><span class="special">)</span>
<span class="special">...</span>    <span class="identifier">def</span> <span class="identifier">class_name</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span>
<span class="special">...</span>       <span class="keyword">return</span> <span class="identifier">self</span><span class="special">.</span><span class="identifier">__class__</span><span class="special">.</span><span class="identifier">__name__</span>
<span class="special">...</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">is_base</span><span class="special">(</span><span class="identifier">Base</span><span class="special">())</span> <span class="special">#</span> <span class="identifier">calls</span> <span class="identifier">the</span> <span class="identifier">class_name</span><span class="special">()</span> <span class="identifier">method</span> <span class="identifier">from</span> <span class="identifier">C</span><span class="special">++</span>
<span class="number">1</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">is_base</span><span class="special">(</span><span class="identifier">Derived</span><span class="special">())</span>
<span class="number">0</span>
</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_call_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function_invocation_and_creation.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_data_members_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
</body>
</html>
